package com.sky.local_ads.adapter;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.internal.is;
import com.sky.local_ads.BusinessListActivity;
import com.sky.local_ads.activity.StoreOffersListActivity;
import com.sky.local_ads.constants.Constants;
import com.sky.local_ads.listener.ServiceResponseListener;
import com.sky.local_ads.servicemanager.ServiceManager;
import com.sky.local_ads.store_list.StoreDetails;
import com.sky.local_ads.store_offers.StoreOfferResponse;
import com.sky.local_ads.store_offers.StoreOffersList;
import com.sky.local_ads.widget.TOProgressDialog;
import com.sky.trueoffers.R;


public class StoreListAdapter extends BaseAdapter {

	private final Activity context;
	private List<StoreDetails> storeList;
	private boolean isService=false;
	private TOProgressDialog progressDialog;

	public StoreListAdapter(Activity context,
			List<StoreDetails> storeArrayList) {
		this.context = context;
		storeList = storeArrayList;
	}

	@Override
	public int getCount() {
		if (storeList != null && storeList.size() > 0) {
			return storeList.size();
		}
		return 0;

	}

	@Override
	public Object getItem(int position) {
		return storeList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return storeList.indexOf(getItem(position));
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		if (convertView == null) {

			convertView = inflater.inflate(R.layout.store_list_row,
					parent, false);
			holder = new ViewHolder();
			holder.storeName = (TextView) convertView
					.findViewById(R.id.storeNameText);
			holder.businessRating=(RatingBar)convertView.findViewById(R.id.storeRatingBar);
			convertView.setTag(holder);

		} else {

			holder = (ViewHolder) convertView.getTag();

		}

		final StoreDetails storeLocator = (StoreDetails) getItem(position);
		
		if(storeLocator!=null) {
			String storeName="LOCAL STORE";
			
			if(storeLocator.getBusinessName()!=null) {
				storeName=storeLocator.getBusinessName();
			}
			holder.storeName.setText(storeName);
			float rating=storeLocator.getRating();
			holder.businessRating.setRating(rating);
			
		}
		
		Button businessOfferButton=(Button)convertView.findViewById(R.id.businessOffer);
		businessOfferButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Toast.makeText(context, "fdsaf", Toast.LENGTH_SHORT).show();
				if(!isService){
					isService=true;
					progressDialog=new TOProgressDialog(context, "hi", "message", false, false);
					progressDialog.show();
					String businessId=storeLocator.getBusinessId();
					ServiceManager.getStoreOffers(context, businessId, offersResponseListener);
				}
			}
		});

		

		return convertView;
	}
	public void updateAdapter(List<StoreDetails> storeList) {
		this.storeList= storeList;
		notifyDataSetChanged();
	}
	private static class ViewHolder {

		private TextView storeName;
		private RatingBar businessRating;
	}
	
private ServiceResponseListener<StoreOfferResponse> offersResponseListener=new ServiceResponseListener<StoreOfferResponse>() {
		
		@Override
		public void onSuccess(StoreOfferResponse response) {
			progressDialog.dismiss();
			isService=false;
			
			if(response!=null && response.getStoreOffersList()!=null && response.getStoreOffersList().size()>0){
				Intent storeOfferIntent=new Intent(context,StoreOffersListActivity.class);
				storeOfferIntent.putExtra(Constants.STORE_OFFER_INTENT, response);
				context.startActivity(storeOfferIntent);
			}
		}
		
		@Override
		public void onFailure(Throwable throwable, String errorResponse) {
			progressDialog.dismiss();
			isService=false;
			// TODO Auto-generated method stub
			
		}
	};

}
